﻿$(document).ready(function() {
	// init
	$('#currentPassword').val('');
	$('#newPassword').val('');
	$('#newPasswordConfirm').val('');

	drawProfile();
	
	
	//====================== Init dialog =====================//
	// change password dialog
	$('#changePasswordDialog').dialog({
		autoOpen: false,
		width: 330,
		modal: true,
		resizable: false,
		buttons:{			
			"Đổi mật khẩu": function(){
				changePassword();
			},
			"Thoát": function(){
				$(this).dialog("close");
			}
		}
	});
	
	
	//============== Helpers ==============//
	function drawProfile()
	{
		$('#profileLink').text('');
		$.get('/xprofile/get-profile', function(data){
			var returnData = $.parseJSON(data);
			$('#profileLink').text(returnData.fullName);
		});	
		
		// add click handler to profile link
		$('#profileLink').click(function(){
			$('#changePasswordDialog').dialog("open");
		});
	}
	
	function changePassword()
	{
		$.post("/xprofile/change-password", $('#changePasswordForm').serialize(), function(data){
			var jsonData = $.parseJSON(data);			
			if (jsonData.code == 0)
			{
				window.location.href = window.location.pathname;
			}
			else
			{
				$("#errorMessage").css("display", "");
				$("#errorMessage td").html(jsonData.data);
			}
		});
	}
});

